package Check;

import java.util.InputMismatchException;
import java.util.Scanner;
public class StudentUI {
    private SuperArray array = new SuperArray();
    private Scanner sc = new Scanner(System.in);
    public StudentUI() {
        array.add(new Student("张飞", "男", 20));
        menu();
    }
    public void menu() {
        System.out.println("欢迎使用学生管理程序");
        while (true) {
            int input=0;
            System.out.println("请选择：1-查看所有学生 2-增加 3-修改 4-删除 5-退出 6-找同年龄 7-找同名 8-同性");
            try{
                System.out.println("请输入数字");
                input=sc.nextInt();             // 临时区 ，占时存 这种类型的值
            }
            catch(InputMismatchException e){
                sc.next();                 //将临时区的值去掉             //第二种  ，sc=new Scanner (System.in);
                System.out.println("请输入正确数字");
                continue;
            }
            //int input = sc.nextInt();
            if (input == 1) {
                show();
            } else if (input == 2) {
                add();
            } else if (input == 3) {
                update();
            } else if (input == 4) {
                remove();
            } else if (input == 5) {
                System.exit(0);
            } else if(  input==6  ){
                sameAge();
            } else if(input==7){
                sameName();
            }else if(input==8)
                sameGender();
            else {
                System.out.println("输入有误");
            }
        }
    }
    public void sameName() {                         //可以查同性别的同学
        int counter = 0;
        while (true) {
            Object[] sameSex = new Object[array.size()];
            System.out.println("请输入姓");
            String name = sc.next();
            if(!name.matches("^[\u4e00-\u9fa5]{1,}$")){
               continue;
           }
            for (int i = 0; i < array.size(); i++) {
                Student student = (Student) array.get(i);
                if (student.getName().matches("^"+name+"[\u4e00-\u9fa5]{1}$")) {
                    System.out.println(student.getName() + "," + student.getGender() + "," + student.getAge());
                    sameSex[counter] = array.get(i);
                    counter++;
                    System.out.println("所有相同名字的学生信息");
                    for (int k = 0; k < counter; k++) {
                        System.out.println(sameSex[k]);
                    }
                }
            }
        }
    }
    public void sameAge() {
        int counter = 0;
        Object[] sameSex = new Object[array.size()];
        int age=0;
        try {
            System.out.println("请输入年龄");
            age=sc.nextInt();
        }catch(InputMismatchException e){
            sc.next();
            System.out.println("请输入合理的年龄");
            return;
        }
        for (int i = 0; i < array.size(); i++) {
            Student student = (Student) array.get(i);
            if (student.getAge() == age) {
                System.out.println(student.getName());
                sameSex[counter] = array.get(i);
                counter++;
            }
        }
        System.out.println("所有相同年龄的学生信息");
        for (int k = 0; k < counter - 1; k++) {
            System.out.println(sameSex[k]);
        }
    }
    public void sameGender(){
        int counter=0;
        Object []  sameSex=new Object[array.size()];
        System.out.println("请输入性别");
        String sex=sc.next();
        for(int i=0;i<array.size();i++){
            Student student=(Student) array.get(i);
            if(student.getGender().equals(sex)) {
                System.out.println(student.getName());
                sameSex[counter] = student;   //   无法赋值
                counter++;
            }
        }
        System.out.println("所有相同"+sex+"性的学生信息");
        for (int j = 0; j < counter - 1; j++) {
            System.out.println(sameSex[j]);
        }
    }
    public void show() {
        for (int i = 0; i < array.size(); i++) {
            Student student = (Student) array.get(i);
            System.out.println(student.getName() + "," + student.getGender() + "," + student.getAge());
        }
    }
    public void add() {
        while(true) {
            System.out.println("请输入学生的性别：");
            String gender = sc.next();
            if(!gender.matches("^[男,女]$")){
                System.out.println("请重新输入有效性别");
                continue;
            }
            System.out.println("请输入学生的姓名：");
            String name = sc.next();
                if(!name.matches("^[\u4e00-\u9fa5]{2,}$")){
                    System.out.println("请重新输入有效姓名");
                    continue;
                }

            int age = 0;
                try {
                        System.out.println("请输入学生的年龄：");
                        age = sc.nextInt();
                }
                catch (InputMismatchException e) {
                    sc.next();
                    System.out.println("增添失败，请重新输入学生的年龄：");
                    return;
                }
                if(age<18 ||age>30){
                    System.out.println("增添失败，请重新输入学生的年龄：");
                    return;
                }

            // int age = sc.nextInt();
            array.add(new Student(name, gender, age));
            return;
        }
    }

    public void update() {
        while (true) {
            System.out.println("请输入需要进行修改的学生姓名");
            String name = sc.next();
            if(!name.matches("^[\u4e00-\u9fa5]{2,}$")){
                System.out.println("请重新输入有效姓名");
                continue;
            }

            for (int i = 0; i < array.size(); i++) {
                Student student = (Student) array.get(i);
                if (student.getName().equals(name)) {
                    while(true) {
                        int mg= 0;
                        try{
                            System.out.println("请选择 1改名字 ，2改性别，3改年龄");
                            mg=sc.nextInt();
                        }
                        catch(InputMismatchException e){
                            sc.next();
                            System.out.println("请重新选择");
                            continue;
                        }
                        if(mg==3) {
                            while(true) {
                                int ag = 0;
                                try {
                                    System.out.println("请输入修改的学生年龄");
                                    ag=sc.nextInt();
                                }
                                catch (InputMismatchException e){
                                    System.out.println("请重新输入需要修改的学生年龄");
                                    sc.next();
                                    return;
                                }
                                if(ag<18 ||ag>30){
                                    System.out.println("增添失败，请重新输入学生的年龄：");
                                    return;
                                }
                                // int newAge = sc.nextInt();
                                student.setAge(ag);
                                System.out.println("年龄信息修改成功");
                                return;
                            }
                        }
                        if(mg==2){
                            System.out.println("请输入修改的学生性别");
                            String newGender=sc.next();
                            if(!newGender.matches("^[男,女]$")){
                                System.out.println("请重新输入有效性别");
                                continue;
                            }
                            student.setGender(newGender);
                            System.out.println("性别信息修改成功");
                            return;
                        }
                        if(mg==1){
                            System.out.println("请输入修改的学生姓名");
                            String newName=sc.next();
                            if(!newName.matches("^[\u4e00-\u9fa5]{2,}$")){
                                System.out.println("请重新输入有效姓名");
                                continue;
                            }
                            student.setName(newName);
                            System.out.println("姓名信息修改成功");
                            return;
                        }
                    }
                }
            }
            System.out.println("名字有误，请重新输入");
        }
    }
    public void remove() {
        while (true) {
            System.out.println("请输入需要删除的学生姓名");
            String name = sc.next();
            for (int i = 0; i < array.size(); i++) {
                Student student = (Student) array.get(i);
                if (!student.getName().equals(name)) {
                    System.out.println("名字有误，请重新输入");
                    break;
                } else {
                    array.remove(i);
                    return;
                }
            }
        }

    }
    public static void main(String[] args) {
        new StudentUI();
    }
}